<?php

namespace App\Services;
use APP\Libraries\Tool;
class SalesService
{
    public function saleB_index($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $good=$Tool->index_first('shopers_goods','ic',$arr_page[$a]->shopers_goods_ic);
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['after_sale']=explode('/',$good->after_sale);
                $array[$a]['business']=$Tool->index_field('shopers','ic',$good->supply_shoper_ic,'title');
                $array[$a]['business_ic']=$good->supply_shoper_ic;
                $array[$a]['classifyIc']=explode('/',$good->goods_classify_ic);
                for($b=0;$b<count($array[$a]['classifyIc']);$b++){
                    $array[$a]['classifyName'][$b]=$Tool->index_field('goods_classifys','ic',$array[$a]['classifyIc'][$b],'name');
                }
                $array[$a]['cover_image']=explode('-',$good->master_graph);
                $array[$a]['describe']=$good->describe;
                $array[$a]['good_identifier']=$good->ic;
                $array[$a]['good_number']=$good->item_number;
                $array[$a]['name']=$good->name;
                $array[$a]['show_state']=$good->upper_shelf;
                $array[$a]['textarea']=$good->details;
                $array[$a]['label']=explode('/',$good->recommend);
                $array[$a]['createat']=$Tool->datas(2,$good->created_at);

                $shopers_goods_specs_list=$Tool->index_list('shopers_goods_specs','shopers_goods_ic','=',$good->ic,'created_at');//规格参数
                for($c=0;$c<count($shopers_goods_specs_list);$c++){
                    $array[$a]['specifications_arr'][$c]['ic']=$shopers_goods_specs_list[$c]->ic;
                    $array[$a]['specifications_arr'][$c]['code']=$shopers_goods_specs_list[$c]->item_number;
                    $array[$a]['specifications_arr'][$c]['factory_price']=$shopers_goods_specs_list[$c]->factory_price;
                    $array[$a]['specifications_arr'][$c]['images']=explode('-',$shopers_goods_specs_list[$c]->picture);
                    $array[$a]['specifications_arr'][$c]['name']=$shopers_goods_specs_list[$c]->name;
                    $array[$a]['specifications_arr'][$c]['retail_price']=$shopers_goods_specs_list[$c]->retail_price;
                    $array[$a]['specifications_arr'][$c]['stock']=$shopers_goods_specs_list[$c]->stock;
                    $array[$a]['specifications_arr'][$c]['supply_price']=$shopers_goods_specs_list[$c]->supply_price;
                }
            }
        }
        return $array;
    }

    public function in_warehouse($input)
    {
        $Tool = new Tool();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $selection_storehouse=$Tool->index_many_first('selection_storehouse','supply_shoper_ic','=',$s_ic,'shopers_goods_ic','=',$input);
        if ($selection_storehouse){//存在
            if($selection_storehouse->soft_deletion==2){//未删
                $Tool->mysql_where_edit('selection_storehouse','ic',$input,['state'=>2,'extension_state'=>2,'extension_data'=>time()*1000]);
            }else{//已删（软删）
                $Tool->mysql_where_edit('selection_storehouse','ic',$input,['state'=>1,'soft_deletion'=>2,'extension_data'=>time()*1000]);
            }
        }else{//不存在
            $good=$Tool->index_first('shopers_goods','ic',$input);
            $date =$Tool->datas(1,'');
            $array['ic']=getfirstic();
            $array['supply_shoper_ic'] = $s_ic;
            $array['goods_classifys_ic'] = $good->goods_classify_ic;
            $array['shopers_goods_ic'] = $good->ic;
            $array['state'] = 1;
            $array['created_at'] = $date;
            $Tool->mysql_insert('selection_storehouse',$array);
        }
        return 'new';
    }

    public function extension_good($input)
    {
        $Tool = new Tool();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        if($input==''){//全部推广（选品库推广）
            $Tool->mysql_three_edit('selection_storehouse','supply_shoper_ic',$s_ic,'state',1,'soft_deletion',2,['state'=>2,'extension_state'=>2,'extension_data'=>time()*1000]);
        }else{//单个立即推广或加入选品库
            $selection_storehouse=$Tool->index_many_first('selection_storehouse','supply_shoper_ic','=',$s_ic,'ic','=',$input);
            if ($selection_storehouse){//存在
                if($selection_storehouse->soft_deletion==2){//未删,立即推广
                    $Tool->mysql_where_edit('selection_storehouse','ic',$input,['state'=>2,'extension_state'=>2,'extension_data'=>time()*1000]);
                }else{//已删（软删）
                    $Tool->mysql_where_edit('selection_storehouse','ic',$input,['state'=>2,'extension_state'=>2,'soft_deletion'=>2,'extension_data'=>time()*1000]);
                }
            }else{//不存在
                $good=$Tool->index_first('shopers_goods','ic',$input);
                $date =$Tool->datas(1,'');
                $array['ic']=getfirstic();
                $array['supply_shoper_ic'] = $s_ic;
                $array['goods_classifys_ic'] = $good->goods_classify_ic;
                $array['shopers_goods_ic'] = $good->ic;
                $array['state'] = 2;
                $array['extension_state'] = 2;
                $array['extension_data'] = time()*1000;
                $array['created_at'] = $date;
                $Tool->mysql_insert('selection_storehouse',$array);
            }
        }
        return 'new';
    }

    public function prepayment_order($arr_page)
    {
        $Tool = new Tool();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $order_line=array();
                //订单基础详情
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['completion_time']=$Tool->datas(2,$arr_page[$a]->complete_data);//完成时间
                $array[$a]['distributor']=$Tool->index_field('shopers','ic',$arr_page[$a]->sales_ic,'title');//直销商名称
                $array[$a]['in_orderNum']=$arr_page[$a]->ic;//内部编号
                $array[$a]['out_orderNum']=$arr_page[$a]->w_order;//外部编号
                $array[$a]['orderNum']=$arr_page[$a]->order;//订单号
                $array[$a]['order_state']=$arr_page[$a]->order_state;//订单状态 2待预付 3已预付 4已完成 5已关闭
                $array[$a]['supplier']=$Tool->index_field('shopers','ic',$arr_page[$a]->supply_ic,'title');//供应商
                $array[$a]['time_prepaid']=$arr_page[$a]->payment_data;//预付时间
                $array[$a]['completion_time']=$arr_page[$a]->complete_data;
                $starttime=strtotime(date("Y-m-d H:i:s",strtotime($arr_page[$a]->created_at)+2*24*60*60))*1000;
                $endtime=strtotime(date("Y-m-d H:i:s"))*1000;
                $timediff = $starttime-$endtime;
                if($timediff>0){
                    $array[$a]['surplus_time']=$timediff;//剩余时间
                }else{
                    $array[$a]['surplus_time']=0;
                }
                $array[$a]['total_factory']=$arr_page[$a]->factory_money;//出厂价
                $array[$a]['total_prepaid']=$arr_page[$a]->prepaid_money;//总价
                $array[$a]['transaction_time']=$Tool->datas(2,$arr_page[$a]->created_at);//下单时间
                $order_line[]=['label'=>'创建订单','time'=>$Tool->datas(2,$arr_page[$a]->created_at)];
                //订单图标情况
                if($arr_page[$a]->payment_state==2){//预付状态时间
                    $order_line[]=['label'=>'已预付','time'=>$arr_page[$a]->payment_data];
                }
                if($arr_page[$a]->after_sales_state==2){//售后状态时间
                    $order_line[]=['label'=>'售后','time'=>$arr_page[$a]->after_sales_data];
                }
                if($arr_page[$a]->complete_state==2){//完成状态时间
                    $order_line[]=['label'=>'已完成','time'=>$arr_page[$a]->complete_data];
                }
                $array[$a]['order_box']['order_line']=$order_line;
                //订单商品详情
                $order_details=$Tool->index_list('order_details','order','=',$arr_page[$a]->order,'created_at');//订单商品详情
                $good=array();
                for($b=0;$b<count($order_details);$b++){
                    $good[$b]['express_name']=$order_details[$b]->express_name;//物流名称
                    $good[$b]['express_order']=$order_details[$b]->express_order;//物流编号
                    $goods=$Tool->index_first('shopers_goods','ic',$order_details[$b]->shopers_goods_ic);//商品
                    $specs=$Tool->index_first('shopers_goods_specs','ic',$order_details[$b]->shopers_goods_specs_ic);//规格
                    $good[$b]['goodsName']=$goods->name;//商品名称
                    $good[$b]['goodsNum']=$goods->ic;//商品编号
                    $good[$b]['num']=$order_details[$b]->number;//购买数量
                    $good[$b]['prepaid_price']=$specs->supply_price;//结算价格
                    $good[$b]['factory_price']=$specs->factory_price;//出厂价格
                    $good[$b]['specifications_name']=$specs->name;//规格名称
                }
                $array[$a]['order_box']['order_messages']=$good;
                //物流信息
                $array[$a]['order_box']['order_user']['address']=$arr_page[$a]->place.'  '.$arr_page[$a]->road;
                $array[$a]['order_box']['order_user']['name']=$arr_page[$a]->name;
                $array[$a]['order_box']['order_user']['remarks']=$arr_page[$a]->remarks;
                $array[$a]['order_box']['order_user']['tel']=$arr_page[$a]->phone;
            }
        }
        $total_prepaid=$Tool->index_two_pluck('orders','sales_ic',$s_ic,'order_state',2,'prepaid_money');
        $arrays['pay_total']=round(array_sum($total_prepaid),2);
        $arrays['array']=$array;
        return $arrays;
    }

    public function money_order_sub($input)
    {
        $Tool = new Tool();
        $array=array();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $date =$Tool->datas(1,'');
        $array['ic']=getfirstic();
        $array['shoper_ic'] = $s_ic;
        $array['recharge_state'] = 1;//充值
        $array['auto_settlement'] = $input['auto_settlement'];
        $array['money'] = $input['money'];
        $array['invoice_state'] = $input['invoice'];
        $array['opening_bank'] = $input['bank'];
        $array['account'] = $input['open_account'];
        $array['bank_account'] = $input['bank_code'];
        $array['bill'] = implode('-',$input['voucher']);
        $array['remarks'] = $input['remarks'];
        $array['created_at'] = $date;
        if(isset($input['ic'])){
            $array['state'] = 1;//状态变更
            $Tool->mysql_edit('recharges',$input['ic'],$array);
        }else{
            $Tool->mysql_insert('recharges',$array);
        }
    }

    public function money_order($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['auto_settlement']=$arr_page[$a]->auto_settlement;
                $array[$a]['bank']=$arr_page[$a]->opening_bank;
                $array[$a]['bank_code']=$arr_page[$a]->bank_account;
                if($arr_page[$a]->recharge_state==1){//1平台充值；2平台扣款 ；3人工充值；4人工扣款
                    $array[$a]['change_type']='sr';
                }elseif($arr_page[$a]->recharge_state==2){
                    $array[$a]['change_type']='sd';
                }elseif($arr_page[$a]->recharge_state==3){
                    $array[$a]['change_type']='ar';
                }elseif($arr_page[$a]->recharge_state==4){
                    $array[$a]['change_type']='ad';
                }
                $array[$a]['handleat']=$arr_page[$a]->recharge_time;
                $array[$a]['invoice']=$arr_page[$a]->invoice_state;
                $array[$a]['money']=$arr_page[$a]->money;
                $array[$a]['open_account']=$arr_page[$a]->account;
                $array[$a]['reject']='';
                $array[$a]['remarks']=$arr_page[$a]->remarks;
                $array[$a]['serial']=$arr_page[$a]->ic;
                if($arr_page[$a]->state==1){
                    $array[$a]['state']='';
                }elseif($arr_page[$a]->state==2){
                    $array[$a]['state']='驳回';
                }elseif($arr_page[$a]->state==3){
                    $array[$a]['state']='通过';
                }
                $array[$a]['voucher']=explode('-',$arr_page[$a]->bill);
                $array[$a]['createat']=$Tool->datas(2,$arr_page[$a]->created_at);
            }
        }
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $money['balance']=round($Tool->index_field('shopers','ic',$s_ic,'total_money'),2);//总价
        $cumulativeRecharge_1=$Tool->index_three_pluck('recharges','shoper_ic',$s_ic,'recharge_state',1,'state',3,'money');
        $cumulativeRecharge_3=$Tool->index_three_pluck('recharges','shoper_ic',$s_ic,'recharge_state',3,'state',3,'money');
        $cumulativeDeduction_2=$Tool->index_three_pluck('recharges','shoper_ic',$s_ic,'recharge_state',2,'state',3,'money');
        $cumulativeDeduction_4=$Tool->index_three_pluck('recharges','shoper_ic',$s_ic,'recharge_state',4,'state',3,'money');
        $money['cumulativeRecharge']=round(array_sum($cumulativeRecharge_1)+array_sum($cumulativeRecharge_3),2);//充值
        $money['cumulativeDeduction']=round(array_sum($cumulativeDeduction_2)+array_sum($cumulativeDeduction_4),2);//扣款
        $arrays['array']=$array;
        $arrays['money']=$money;
        return $arrays;
    }

    public function saleB_invoice()
    {
        $Tool = new Tool();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $shoper=$Tool->index_first('shopers','ic',$s_ic);
        $array['ic']=$shoper->ic;
        $array['code']=$shoper->kh_identification_number;
        $array['bank_account']=$shoper->kh_account;
        $array['type']=$shoper->kh_invoice_state;
        $array['bank']=$shoper->kh_opening_bank;
        $array['tel']=$shoper->kh_mobile;
        $array['name']=$shoper->title;
        $array['address']=$shoper->kh_place;
        return $array;
    }

    public function saleB_business()
    {
        $Tool = new Tool();
        $s_ic=$Tool->jwts()->s_ic;//直销商ic
        $shoper=$Tool->index_first('shopers','ic',$s_ic);
        $array['ic']=$shoper->ic;
        $array['address']=$shoper->lx_place;
        $array['code']=$shoper->zj_credit_code;
        $array['contacts']=$shoper->lx_contacts;
        $array['name']=$shoper->title;
        $array['registere']=$shoper->zj_place;
        $array['tel']=$shoper->lx_mobile;
        $array['term']=explode('-',$shoper->zj_term);
        return $array;
    }

}